package com.lq.javawebTest.core.entity;

import com.lq.javawebTest.core.utils.HttpResponseHandler;

import java.io.OutputStream;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;


/**
 * 封装一个响应对象
 */
public class HttpResponse implements Serializable {
    public static final long serialVersionUID = 1L;
    // 协议
    private String protocol = "Http/1.1";
    // 响应码
    private String code = "200";
    // 信息
    private String message = "ok";
    // 响应头
    private Map<String,String> header = new HashMap<>();
    // 响应体
    private String body;

    //持有一个流
    private OutputStream outputStream;

    public OutputStream getOutputStream() {
        return outputStream;
    }

    public void setOutputStream(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public HttpResponse() {
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public Map<String, String> getHeader() {
        return header;
    }

    public void setHeader(String key, String value) {
        this.header.put(key, value);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return "HttpResponse{" +
                "protocol='" + protocol + '\'' +
                ", code='" + code + '\'' +
                ", message='" + message + '\'' +
                ", header=" + header +
                ", body='" + body + '\'' +
                ", outputStream=" + outputStream +
                '}';
    }

    //通用的成功响应
    public static void success(HttpResponse httpResponse, String message) {
//        HttpResponse httpResponse = new HttpResponse();
        String body = "<h1>"+message+"</h1>";
        httpResponse.setCode("200");
        httpResponse.setHeader("Content-Type","text/html;charset=UTF-8");
        httpResponse.setHeader("Content-Length",Integer.toString(body.getBytes().length));
        httpResponse.setBody(body);
        HttpResponseHandler.writeResponse(httpResponse, httpResponse.getOutputStream());
    }

    //通用的失败响应
    public static void error(HttpResponse httpResponse, String message) {
//        HttpResponse httpResponse = new HttpResponse();
        String body = "<h1>"+message+"</h1>";
        httpResponse.setCode("500");
        httpResponse.setMessage("Internal Server Error");
        httpResponse.setHeader("Content-Type","text/html;charset=UTF-8");
        httpResponse.setHeader("Content-Length",Integer.toString(body.getBytes().length));
        httpResponse.setBody(body);
        HttpResponseHandler.writeResponse(httpResponse, httpResponse.getOutputStream());
    }

    //通用的失败响应
    public static void fail(HttpResponse httpResponse, String message) {
//        HttpResponse httpResponse = new HttpResponse();
        String body = "<h1>"+message+"</h1>";
        httpResponse.setCode("403");
        httpResponse.setMessage("Internal Server Error");
        httpResponse.setHeader("Content-Type","text/html;charset=UTF-8");
        httpResponse.setHeader("Content-Length",Integer.toString(body.getBytes().length));
        httpResponse.setBody(body);
        HttpResponseHandler.writeResponse(httpResponse, httpResponse.getOutputStream());
    }

    //重定向响应
    public static void redirect(HttpResponse httpResponse, String url) {
//        HttpResponse httpResponse = new HttpResponse();
        httpResponse.setCode("302");
        httpResponse.setMessage("Moved Temporarily");
        httpResponse.setHeader("Location",url);
        HttpResponseHandler.writeResponse(httpResponse, httpResponse.getOutputStream());
    }


}
